package cn.edu.nju.software.android.model;


import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import cn.edu.nju.software.android.activity.MainActivity;

import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.AccountAPI;
import com.weibo.sdk.android.api.FriendshipsAPI;
import com.weibo.sdk.android.api.UsersAPI;
import com.weibo.sdk.android.net.RequestListener;

public class User implements Serializable{
	public static FriendshipsAPI api = new FriendshipsAPI(MainActivity.accessToken);
	public static AccountAPI accountAPI = new AccountAPI(MainActivity.accessToken);
	public static UsersAPI userAPI = new UsersAPI(MainActivity.accessToken);
	private static final long serialVersionUID = -8698464480172324006L;
	private long id;
	private String name;
	private String screenName;
	private String province;
	private String city;
	private String location;
	private String description;
	private String profileImageUrl;
	private String url;
	private String gender;
	private boolean isProtected;
	private int followersCount;
	private int friendsCount;
	private int statusesCount;
	private int favouritesCount;
	private boolean following;
	private boolean verified;
	private long weiboID;
	private String avatar_large;


	public static List<User> constructUsers(String response) {
		ArrayList<User> users = new ArrayList<User>();
		JSONArray json;
		try {
			JSONObject object = new JSONObject(response);
			json = object.getJSONArray("users");
			for (int i = 0; i < json.length(); i++) {
				User u = new User(json.getJSONObject(i));
				users.add(u);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return users;
	}

	public User(JSONObject json){
		try {
			id = json.getLong("id");
			name = json.getString("name");
			screenName = json.getString("screen_name");
			location = json.getString("location");
			province = json.getString("province");
			city = json.getString("city");
			description = json.getString("description");
			profileImageUrl = json.getString("profile_image_url");
			url = json.getString("url");
			gender = json.getString("gender");
			if(!json.isNull("protected"))
				isProtected = json.getBoolean("protected");
			followersCount = json.getInt("followers_count");
			friendsCount = json.getInt("friends_count");
			favouritesCount = json.getInt("favourites_count");
			statusesCount = json.getInt("statuses_count");
			following = json.getBoolean("following");
			verified = json.getBoolean("verified");
			if(!json.isNull("status"))
				weiboID = json.getJSONObject("status").getLong("id");
			if(!json.isNull("avatar_large"))
				avatar_large = json.getString("avatar_large");
		} catch (JSONException jsone) {
			jsone.printStackTrace();
		}
	}


	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getScreenName() {
		return screenName;
	}
	public void setScreenName(String screenName) {
		this.screenName = screenName;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getProfileImageUrl() {
		return profileImageUrl;
	}
	public void setProfileImageUrl(String profileImageUrl) {
		this.profileImageUrl = profileImageUrl;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public boolean isProtected() {
		return isProtected;
	}
	public void setProtected(boolean isProtected) {
		this.isProtected = isProtected;
	}
	public int getFollowersCount() {
		return followersCount;
	}
	public void setFollowersCount(int followersCount) {
		this.followersCount = followersCount;
	}
	public int getFriendsCount() {
		return friendsCount;
	}
	public void setFriendsCount(int friendsCount) {
		this.friendsCount = friendsCount;
	}
	public int getStatusesCount() {
		return statusesCount;
	}
	public void setStatusesCount(int statusesCount) {
		this.statusesCount = statusesCount;
	}
	public int getFavouritesCount() {
		return favouritesCount;
	}
	public void setFavouritesCount(int favouritesCount) {
		this.favouritesCount = favouritesCount;
	}
	public boolean isFollowing() {
		return following;
	}
	public void setFollowing(boolean following) {
		this.following = following;
	}
	public boolean isVerified() {
		return verified;
	}
	public void setVerified(boolean verified) {
		this.verified = verified;
	}


	public long getWeiboID() {
		return weiboID;
	}


	public void setWeiboID(long weiboID) {
		this.weiboID = weiboID;
	}

	public String getAvatar_large() {
		return avatar_large;
	}

	public void setAvatar_large(String avatar_large) {
		this.avatar_large = avatar_large;
	}

	public static void downloadFriendsList(long uid, int cursor, final Handler handler, final List<User> friends){
		api.friends(uid, 50, cursor, false, new RequestListener() {

			@Override
			public void onIOException(IOException e) {
				System.out.println(e);
			}

			@Override
			public void onError(WeiboException e) {
				System.out.println(e);
			}

			@Override
			public void onComplete(String response) {
				List<User> newUsers = User.constructUsers(response);
				for(int i = 0; i < newUsers.size() ; i++){
					friends.add(newUsers.get(i));
				}
				handler.sendEmptyMessage(0);
			}
		});
	}

	public static void downloadFollowersList(long uid, int cursor, final Handler handler, final List<User> followers){
		api.followers(uid, 50, cursor, false, new RequestListener() {

			@Override
			public void onIOException(IOException e) {
				System.out.println(e);
			}

			@Override
			public void onError(WeiboException e) {
				System.out.println(e);
			}

			@Override
			public void onComplete(String response) {
				List<User> newUsers = User.constructUsers(response);
				for(int i = 0; i < newUsers.size() ; i++){
					followers.add(newUsers.get(i));
				}
				handler.sendEmptyMessage(0);
			}
		});
	}

	public static void getUID(final Handler handler){
		accountAPI.getUid(new RequestListener() {

			@Override
			public void onIOException(IOException e) {
				System.out.println(e);
			}

			@Override
			public void onError(WeiboException e) {
				System.out.println(e);
			}

			@Override
			public void onComplete(String response) {
				try {
					JSONObject obejct = new JSONObject(response);
					Message msg = new Message();
					Bundle data = new Bundle();
					data.putLong("uid", obejct.getLong("uid"));
					msg.setData(data);
					handler.sendMessage(msg);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void getUserInfo(long uid,  final Handler handler){
		userAPI.show(uid, new RequestListener() {

			@Override
			public void onIOException(IOException e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(WeiboException e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onComplete(String response) {
				JSONObject object;
				try {
					object = new JSONObject(response);
					User user = new User(object);
					Bundle data = new Bundle();
					data.putSerializable("user", user);
					Message msg = new Message();
					msg.setData(data);
					handler.sendMessage(msg);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	public void createFriendship(final Handler handler){
		api.create(id, screenName, new RequestListener() {

			@Override
			public void onIOException(IOException e) {

			}

			@Override
			public void onError(WeiboException e) {

			}

			@Override
			public void onComplete(String response) {
				handler.sendEmptyMessage(0);
			}
		});
	}

	public void destroyFriendship(final Handler handler){
		api.destroy(id, screenName, new RequestListener() {

			@Override
			public void onIOException(IOException e) {

			}

			@Override
			public void onError(WeiboException e) {

			}

			@Override
			public void onComplete(String response) {
				handler.sendEmptyMessage(0);
			}
		});
	}

}
